package com.yuanfeng.job.job;



import com.yuanfeng.commoms.util.DateUtils;
import com.yuanfeng.commoms.vo.job.AcquiescenceVO;
import com.yuanfeng.job.service.UpdateRecordStatusService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * 确认收货超过14天默认默认好评
 */
public class AcquiescenceJob implements BaseJob {

    @Autowired
    UpdateRecordStatusService updateRecordStatusService;

    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        List<AcquiescenceVO> list = updateRecordStatusService.queryNotEvaluatedOrder();
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd");
        try{
            for(AcquiescenceVO order : list){
                //获取用户信息
                AcquiescenceVO userInfo = updateRecordStatusService.queryUserInfo(order.getBuyerUserId());
                if(userInfo == null || "".equals(userInfo)){
                    continue;
                }
                order.setUserName(userInfo.getUserName());
                if(userInfo.getUserAvatar() ==null ){
                    order.setUserAvatar(" ");
                }else {
                    order.setUserAvatar(userInfo.getUserAvatar());
                }
                List<AcquiescenceVO> goodsInfo= updateRecordStatusService.queryGoodsInfo(order.getOrderId());
                if(goodsInfo.size()==0){
                    continue;
                }
                order.setGoodsId(goodsInfo.get(0).getGoodsId());
                order.setCommonId(goodsInfo.get(0).getCommonId());
                order.setGoodsName(goodsInfo.get(0).getGoodsName());
                order.setGoodsSpec(goodsInfo.get(0).getGoodsSpec());

                order.setScores(5);
                order.setResult("good");
                order.setContent("此用户未填写评价内容");
                order.setIsanonymous(0);

                order.setStatus(1);
                order.setEvaluationFrom(5);
                String shouHuoshijian = updateRecordStatusService.queryShouHuoshijian(order.getOrderId());
                if(shouHuoshijian==null || "".equals(shouHuoshijian)){
                    continue;
                }else{

                    Date parse = dateformat.parse(shouHuoshijian);
                    cal.setTime(parse);
                    //增加7天
                    cal.add(Calendar.DAY_OF_MONTH, 6);
                    //Calendar转为Date类型
                    Date date=cal.getTime();
                    String format1 = dateformat1.format(date);
                    Date date1 = DateUtils.randomDate(format1+" 06:00:00",format1+" 24:00:00");
                    String finishTime = dateformat.format(date1);

                    order.setCreateTime(finishTime);

                }
                Integer integer = updateRecordStatusService.insertEvaluated(order);
                if(integer ==1){
                    updateRecordStatusService.updateOrderEvaluated(order.getOrderId());
                }

            }

        } catch(Exception e){
            e.printStackTrace();
        }
    }
}
